﻿using System;
using System.Xml;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Collections.Generic;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
using System.Windows.Browser;
//using ComponentArt.Silverlight.UI.Navigation;

namespace MapEditor
{
    public partial class Page : UserControl
    {
        private SLGameEngine.Engine _engine;
        private SLGameEngine.TerrainMgr _terrainMgr;
        private SLGameEngine.Editor _editor;
        private int _currentLayer = 0;
        private int _currentScaleValue = 0;
        private double _normalWidth = 0.0;
        private double _normalHeight = 0.0;

        private DlgNewMap _dlgNewMap = null;
        private double _appWidth = 0;
        private double _appHeight = 0;
        private Image _cursorImage = new Image();
        private SLGameEngine.ObjectBase _previewObject = null;
        private SLGameEngine.ObjectBase _selectedObject = null;
        private bool _dragDrop = false;
        private Point _selectionPoint = new Point();
        private OpacityResource _selectedOpacityResource;

        private bool _gameMode = false;

        public Page()
        {
            InitializeComponent();
            HtmlPage.Document.AttachEvent("oncontextmenu", this.OnRightClick);
            this.Loaded += new RoutedEventHandler(Page_Loaded);
            App.Current.Host.Content.Resized += new EventHandler(Content_Resized);
        }
        private void OnRightClick(object sender, HtmlEventArgs e)
        {
            e.PreventDefault();
            StackMenu.Visibility = Visibility.Visible;

            StackMenu.Margin = new Thickness(e.ClientX, e.ClientY, 0, 0);
        }


        void Content_Resized(object sender, EventArgs e)
        {
            _appWidth = App.Current.Host.Content.ActualWidth;
            _appHeight = App.Current.Host.Content.ActualHeight;

            if (_appWidth != 0 && _appHeight != 0)
            {
                MapArea.Width = MapScroller.Width = _appWidth - SLGameEngine.Consts.ImageToolbarWidth;
                MapArea.Height = MapScroller.Height = _appHeight - SLGameEngine.Consts.ToolbarHeight;
                ToolbarScroller.Height = _appHeight - SLGameEngine.Consts.ToolbarHeight;
              //  MainToolbar.Width = _appWidth - SLGameEngine.Consts.ImageToolbarWidth;
                Title.SetValue(Canvas.LeftProperty, (double)_appWidth - 200);
                TitleReflect.SetValue(Canvas.LeftProperty, (double) _appWidth - 200);

                _normalWidth = MapArea.Width;
                _normalHeight = MapArea.Height;
            }
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            ObjectTemplateManager.LoadTemplateObjects(new ObjectLoadComplete(OnObjectLoadComplete));
            //ObjProperties.Loaded += new RoutedEventHandler(ObjProperties_Loaded);         

            _currentScaleValue = (int)ScalePickerSlider.Value;
        }

        public void OnObjectLoadComplete()
        {

            TreeViewItem parentNode = Utils.CreateObjectTemplateTree(ObjectTemplateManager.ObjectBaseTemplates, typeof(SLGameEngine.ObjectTerrain));
            parentNode.Header = "Terrain Objects";            
            TreeViewObjects.Items.Add(parentNode);
            parentNode.IsExpanded = true;

            parentNode = Utils.CreateObjectTemplateTree(ObjectTemplateManager.ObjectBaseTemplates, typeof(SLGameEngine.ObjectGame));
            parentNode.Header = "Game Objects";
            TreeViewObjects.Items.Add(parentNode);
            parentNode.IsExpanded = true;

            parentNode = Utils.CreateObjectTemplateTree(ObjectTemplateManager.ObjectBaseTemplates, typeof(SLGameEngine.ObjectMap));
            parentNode.Header = "Map Objects";
            TreeViewObjects.Items.Add(parentNode);
            parentNode.IsExpanded = true;

            parentNode = Utils.CreateObjectTemplateTree(ObjectTemplateManager.ObjectBaseTemplates, typeof(SLGameEngine.ObjectCreature));
            parentNode.Header = "Creature Objects";
            TreeViewObjects.Items.Add(parentNode);
            parentNode.IsExpanded = true;

            _cursorImage.IsHitTestVisible = false;
            _cursorImage.SetValue(Canvas.ZIndexProperty, 10000);
            _cursorImage.Visibility = Visibility.Collapsed;
            MainCanvas.Children.Add(_cursorImage);

            EnterMode(SLGameEngine.Enums.PaintMethod.Draw);

            OpacityMasksManager.LoadMasksResources(new ObjectLoadComplete(OnOpacityLoadComplete));

        }

        public void OnOpacityLoadComplete()
        {
            TreeViewItem parentNode = Utils.CreateOpacityTree();
            parentNode.Header = "Opacity Masks";
            parentNode.IsExpanded = true;
            OpacityMaskTreeView.Items.Add(parentNode);

            _engine = new SLGameEngine.Engine();
            _engine.Initialize(MapCanvas, ObjectTemplateManager.ObjectBaseTemplates);

            _terrainMgr = _engine.GetTerrainMgr();
            _terrainMgr.CreateNewMap(SLGameEngine.Consts.DefaultMapWidth, SLGameEngine.Consts.DefaultMapHeight, SLGameEngine.Consts.DefaultTileRadius, "New Map", SLGameEngine.Consts.TerrainLayerCount);
            MapCanvas.Height = _terrainMgr.TerrainHeightInTiles * _terrainMgr.TerrainTileRadius;
            MapCanvas.Width = _terrainMgr.TerrainWidthInTiles * _terrainMgr.TerrainTileRadius;

            _editor = _engine.GetEditor();

            this.KeyDown += new KeyEventHandler(Page_KeyDown);
            
        }

        //void ObjProperties_Loaded(object sender, RoutedEventArgs e)
        //{
        //    ObjProperties.Columns[0].IsReadOnly = true;
        //    ObjProperties.Columns[0].Width = new DataGridLength(100);
        //    ObjProperties.Columns[1].Width = new DataGridLength(100);
        //}

        void Page_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Delete:
                    if (null != _selectedObject)
                        _editor.DeleteObject(_selectedObject);
                    break;
                default:
                    if (_gameMode)
                    {
                        _engine.LocalPlayer.KeyDown(sender, e);
                    }
                    break;
            }
        }

        private void OpacityMaskTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (OpacityMaskTreeView.SelectedItem != null)
            {
                if (null != _previewObject && PreviewCanvas.Children.Contains(_previewObject))
                    PreviewCanvas.Children.Remove(_previewObject);
                _previewObject = null;

                if (null != _selectedOpacityResource && PreviewCanvas.Children.Contains(_selectedOpacityResource.OpacityImage))
                    PreviewCanvas.Children.Remove(_selectedOpacityResource.OpacityImage);

                TreeViewItem node = (TreeViewItem) OpacityMaskTreeView.SelectedItem;
                _selectedOpacityResource = (OpacityResource)node.Tag;

                if (null != _selectedOpacityResource)
                {
                    
                    _selectedOpacityResource.OpacityImage.SetValue(Canvas.LeftProperty, (double)32);
                    _selectedOpacityResource.OpacityImage.SetValue(Canvas.TopProperty, (double)1);
                    PreviewCanvas.Children.Add(_selectedOpacityResource.OpacityImage);
                }
            }
        }

        //private void OpacityMaskTreeView_NodeSelected(object sender, TreeViewNodeMouseEventArgs e)
        //{
        //    if (OpacityMaskTreeView.Selection.Count > 0)
        //    {
        //        if (null != _previewObject && PreviewCanvas.Children.Contains(_previewObject))
        //            PreviewCanvas.Children.Remove(_previewObject);
        //        _previewObject = null;

        //        if (null != _selectedOpacityResource && PreviewCanvas.Children.Contains(_selectedOpacityResource.OpacityImage))
        //            PreviewCanvas.Children.Remove(_selectedOpacityResource.OpacityImage);

        //        TreeViewNode node = OpacityMaskTreeView.Selection[0];
        //        _selectedOpacityResource = (OpacityResource)node.Tag;

        //        if (null != _selectedOpacityResource)
        //        {
        //            _selectedOpacityResource.OpacityImage.SetValue(Canvas.LeftProperty, (double)32);
        //            _selectedOpacityResource.OpacityImage.SetValue(Canvas.TopProperty, (double)1);
        //            PreviewCanvas.Children.Add(_selectedOpacityResource.OpacityImage);
        //        }
        //    }
        //}

        //private void ImageTreeView_NodeSelected(object sender, TreeViewNodeMouseEventArgs e)
        //{
        //    if (TreeViewObjects.Selection.Count > 0)
        //        ShowPreviewNode((SLGameEngine.ObjectBase)TreeViewObjects.Selection[0].Tag);
        //}

        private void TreeViewObjects_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            System.Windows.Controls.TreeViewItem tvi = (System.Windows.Controls.TreeViewItem)TreeViewObjects.SelectedItem;
            if (null != tvi)
                ShowPreviewNode((SLGameEngine.ObjectBase)tvi.Tag);
        }

        private void ShowPreviewNode(SLGameEngine.ObjectBase treeViewObj)
        {
            if (null != treeViewObj)
            {
                if (null != _previewObject && PreviewCanvas.Children.Contains(_previewObject))
                    PreviewCanvas.Children.Remove(_previewObject);
                if (null != _selectedOpacityResource && PreviewCanvas.Children.Contains(_selectedOpacityResource.OpacityImage))
                    PreviewCanvas.Children.Remove(_selectedOpacityResource.OpacityImage);

                _selectedOpacityResource = null;
                _previewObject = treeViewObj.Clone(SLGameEngine.Consts.PreviewRadius);
                _previewObject.ShowPreview();
                if (null != _previewObject)
                {
                    if (_previewObject.GetType() == typeof(SLGameEngine.ObjectCreature))
                        ((SLGameEngine.ObjectCreature)_previewObject).Animate(SLGameEngine.Enums.Direction.South, SLGameEngine.Enums.AnimationType.Walk, false);
                    _previewObject.SetValue(Canvas.LeftProperty, (double)51);
                    _previewObject.SetValue(Canvas.TopProperty, (double)1);
                    PreviewCanvas.Children.Add(_previewObject);
                }
            }
        }

        private void SaveMap()
        {
            _editor.SaveMap();
            StackMenu.Visibility = Visibility.Collapsed;
        }

        private void OpenMap()
        {
            OpenFileDialog opf = new OpenFileDialog();
            opf.Filter = "map files (*.xml)|*.xml|All files (*.*)|*.*";
            opf.ShowDialog();

            if (null != opf.File)
            {
                System.IO.Stream stream = opf.File.OpenRead();
                _editor.OpenMap(stream);
                MapCanvas.Height = _terrainMgr.TerrainHeightInTiles * _terrainMgr.TerrainTileRadius;
                MapCanvas.Width = _terrainMgr.TerrainWidthInTiles * _terrainMgr.TerrainTileRadius;
            }
            StackMenu.Visibility = Visibility.Collapsed;
        }

        private void CreateNewMap()
        {
            if (null == _dlgNewMap)
            {
                _dlgNewMap = new DlgNewMap(new InvokeCreateNewMap(OnCreateNewMap));
                double posX = (_appWidth / 2) - (_dlgNewMap.Width / 2);
                double posY = (_appHeight / 2) - (_dlgNewMap.Height / 2);
                _dlgNewMap.SetValue(Canvas.LeftProperty, posX);
                _dlgNewMap.SetValue(Canvas.TopProperty, posY);
                _dlgNewMap.SetValue(Canvas.ZIndexProperty, 1);
                MainCanvas.Children.Add(_dlgNewMap);
                StackMenu.Visibility = Visibility.Collapsed;
            }
        }

        private void OnCreateNewMap(bool success)
        {
            if (null != _dlgNewMap)
            {
                if (true == success)
                {
                    int width = _dlgNewMap.WidthInTiles;
                    int height = _dlgNewMap.HeightInTiles;
                    int tileRadius = _dlgNewMap.TileRadius;
                    string mapName = _dlgNewMap.MapName;

                    _terrainMgr.CreateNewMap(width, height, tileRadius, mapName, SLGameEngine.Consts.TerrainLayerCount);

                    MapCanvas.Height = _terrainMgr.TerrainHeightInTiles * _terrainMgr.TerrainTileRadius;
                    MapCanvas.Width = _terrainMgr.TerrainWidthInTiles * _terrainMgr.TerrainTileRadius;
                }
                MainCanvas.Children.Remove(_dlgNewMap);
                _dlgNewMap = null;
            }
        }

        //private void MainMenu_MenuClick(object sender, Menu.MenuClickEventArgs mce)
        //{
        //    if ((MenuItem)mce.clickSource == this.MenuItemNewMap)
        //        CreateNewMap();
        //    else if ((MenuItem)mce.clickSource == this.MenuItemOpenMap)
        //        OpenMap();
        //    else if ((MenuItem)mce.clickSource == this.MenuItemSaveMap)
        //        SaveMap();
        //}

        private void EnterMode(SLGameEngine.Enums.PaintMethod mode)
        {
            UpdateModeSelection(mode);

            if (null != _selectedObject && mode != SLGameEngine.Enums.PaintMethod.Select)
                _selectedObject.Deselect();
            SLGameEngine.Consts.PaintMethod = mode;
            SetCursor();
        }

        private void SetCursor()
        {
            string fileName = String.Empty;
            switch (SLGameEngine.Consts.PaintMethod)
            {
                case SLGameEngine.Enums.PaintMethod.Select:
                    this.Cursor = Cursors.Arrow;
                    _cursorImage.Source = null;
                    return;
                case SLGameEngine.Enums.PaintMethod.Draw:
                    fileName = "Images/toolbar/Draw.png";
                    break;
                case SLGameEngine.Enums.PaintMethod.Fill:
                    fileName = "Images/toolbar/Fill.png";
                    break;
                case SLGameEngine.Enums.PaintMethod.Dropper:
                    fileName = "Images/toolbar/Dropper.png";
                    break;
                case SLGameEngine.Enums.PaintMethod.Erase:
                    fileName = "Images/toolbar/Erase.png";
                    break;
            }
            if (String.Empty != fileName)
            {
                ImageSource imgSrc = SLGameEngine.Utils.LoadImage(fileName);
                _cursorImage.Source = imgSrc;
            }
        }

        private void Canvas_MouseEnter(object sender, MouseEventArgs e)
        {
            if ((_previewObject != null || _selectedOpacityResource != null) && SLGameEngine.Consts.PaintMethod != SLGameEngine.Enums.PaintMethod.Select)
            {
                this.Cursor = Cursors.None;
                _cursorImage.Visibility = Visibility.Visible;
            }
        }

        private void Canvas_MouseLeave(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Arrow;
            _cursorImage.Visibility = Visibility.Collapsed;
        }

        private void MapArea_MouseMove(object sender, MouseEventArgs e)
        {
            Point ptApp = e.GetPosition(null);
            Point ptMap = e.GetPosition(MapCanvas);

            _cursorImage.SetValue(Canvas.LeftProperty, ptApp.X);
            _cursorImage.SetValue(Canvas.TopProperty, ptApp.Y - 16); // pencil points 16 pixels down
            _cursorImage.SetValue(Canvas.ZIndexProperty, (int)(ptMap.Y + _cursorImage.ActualHeight));

            if (true == _dragDrop)
            {
                _selectedObject.CurrentX = ptMap.X - _selectionPoint.X;
                _selectedObject.CurrentY = ptMap.Y - _selectionPoint.Y;

                _selectedObject.SetValue(Canvas.ZIndexProperty, (int)(ptMap.Y + _selectedObject.Height));
            }
        }

        private void MapCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _dragDrop = false;
        }
        
        private void UpdateSelectedProperties()
        {
            PropertiesTreeView.Items.Clear();

            TreeViewItem childNode = new TreeViewItem();
            childNode.Tag = null;
            childNode.Header = "Base";
            PropertiesTreeView.Items.Add(childNode);

            // Add a listview with all the properties
            // Format: Name | Value

/*
            ListBox lb = new ListBox();
            ListBoxItem lbItem = new ListBoxItem();
            lbItem.A
            lb.Items.Add(lbItem);
*/                
            childNode = new TreeViewItem();
            childNode.Tag = null;
            childNode.Header = "Name = " + _selectedObject.ObjectName;
            PropertiesTreeView.Items.Add(childNode);

            childNode = new TreeViewItem();
            childNode.Tag = null;
            childNode.Header = "Desc = " + _selectedObject.Description;
            PropertiesTreeView.Items.Add(childNode);

            childNode = new TreeViewItem();
            childNode.Tag = null;
            childNode.Header = "IsWalkable = " + _selectedObject.IsWalkable.ToString();
            PropertiesTreeView.Items.Add(childNode);

            childNode = new TreeViewItem();
            childNode.Tag = null;
            childNode.Header = "IsTargetable = " + _selectedObject.IsTargetable.ToString();
            PropertiesTreeView.Items.Add(childNode);

            childNode = new TreeViewItem();
            childNode.Tag = null;
            childNode.Header = "IsAttackable = " + _selectedObject.IsAttackable.ToString();
            PropertiesTreeView.Items.Add(childNode);
        }

        private void MapCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Point pt = e.GetPosition(MapCanvas);

            if (SLGameEngine.Consts.PaintMethod == SLGameEngine.Enums.PaintMethod.Select)
            {
                SLGameEngine.ObjectBase obj = _editor.SelectObject(pt, ref _selectionPoint);
                if (null != _selectedObject && obj != _selectedObject)
                    _selectedObject.Deselect();

                if (null != obj)
                {
                    _selectedObject = obj.Select();
                    if (_selectedObject.GetType() != typeof(SLGameEngine.ObjectTerrain))
                        _dragDrop = true;

                    UpdateSelectedProperties();
                }
            }

            if (SLGameEngine.Consts.PaintMethod == SLGameEngine.Enums.PaintMethod.Draw)
            {
                if (null != _previewObject)
                {
                    if (null != _selectedObject)
                        _selectedObject.Deselect();

                    if (typeof(SLGameEngine.ObjectTerrain) == _previewObject.GetType())
                        _selectedObject = _editor.AddTerrainObject(pt, _currentLayer, (SLGameEngine.ObjectTerrain)_previewObject);
                    else
                        _selectedObject = _editor.DrawObject(pt.X, pt.Y, _previewObject);
                    if (null != _selectedObject)
                        _selectedObject.Select();

                    UpdateSelectedProperties();
                }
                else if (null != _selectedOpacityResource)
                    _editor.ApplyOpacityMask(pt, _currentLayer, _selectedOpacityResource.OpacityMaskID, _selectedOpacityResource.OpacityImage);
            }
            else if (SLGameEngine.Consts.PaintMethod == SLGameEngine.Enums.PaintMethod.Erase)
            {
                SLGameEngine.ObjectBase obj = _editor.SelectObject(pt, ref _selectionPoint);
                if (null != obj)
                {
                    obj.Deselect();
                    _editor.DeleteObject(obj);
                }
            }
            else if (SLGameEngine.Consts.PaintMethod == SLGameEngine.Enums.PaintMethod.Fill)
            {
                if (null != _previewObject && _previewObject.GetType() == typeof(SLGameEngine.ObjectTerrain))
                    _editor.Fill(pt.X, pt.Y, _currentLayer, (SLGameEngine.ObjectTerrain)_previewObject);
            }
            else if (SLGameEngine.Consts.PaintMethod == SLGameEngine.Enums.PaintMethod.Dropper)
            {
                SLGameEngine.ObjectBase obj = _editor.SelectObject(pt, ref _selectionPoint);
                if (null != obj)
                {
                    int baseObjID = obj.BaseObjectID;
                    if (obj.GetType() == typeof(SLGameEngine.ObjectTerrain))
                        baseObjID = ((SLGameEngine.ObjectTerrain)obj).GetLayerBaseObjectID(_currentLayer);

                    foreach (TreeViewItem parentNode in TreeViewObjects.Items)
                    {
                        foreach (TreeViewItem node in parentNode.Items)
                        {
                            if (node.Tag != null)
                            {
                                SLGameEngine.ObjectBase treeObj = (SLGameEngine.ObjectBase)node.Tag;
                                if (baseObjID == treeObj.BaseObjectID)
                                {
                                    node.IsSelected = true;
                                    ShowPreviewNode(treeObj);
                                    return;
                                }
                            }
                        }
                    }
                }
                string resource = _editor.Dropper(pt, _currentLayer);
            }
        }

        private void UpdateModeSelection(SLGameEngine.Enums.PaintMethod mode)
        {
            switch (mode)
            {
                case SLGameEngine.Enums.PaintMethod.Select:
                    SelectionBox.SetValue(Canvas.LeftProperty, (double)-2);
                    break;
                case SLGameEngine.Enums.PaintMethod.Draw:
                    SelectionBox.SetValue(Canvas.LeftProperty, (double)23);
                    break;
                case SLGameEngine.Enums.PaintMethod.Dropper:
                    SelectionBox.SetValue(Canvas.LeftProperty, (double)48);
                    break;
                case SLGameEngine.Enums.PaintMethod.Fill:
                    SelectionBox.SetValue(Canvas.LeftProperty, (double)73);
                    break;
                case SLGameEngine.Enums.PaintMethod.Erase:
                    SelectionBox.SetValue(Canvas.LeftProperty, (double)98);
                    break;
                default:
                    break;
            }
        }

        private void LayerPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (null != LayerPicker)
                _currentLayer = LayerPicker.SelectedIndex;
        }

        //private void ScalePicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    if (null != ScalePicker)
        //    {
        //        _currentScaleValue = ScalePicker.SelectedIndex;
        //        double scalingFactor = System.Convert.ToDouble(((ComboBoxItem)ScalePicker.SelectedItem).Tag);
        //        // Need to process "all" factor.
        //        if (scalingFactor <= 0.0)
        //            return;

        //        MacScale.ScaleX = MacScale.ScaleY = scalingFactor;
        //        MapScroller.Width = MapArea.Width = _normalWidth * (1.0 / scalingFactor);
        //        MapScroller.Height = MapArea.Height = _normalHeight * (1.0 / scalingFactor);
        //    }
        //}
               
        private void ImageSelect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            EnterMode(SLGameEngine.Enums.PaintMethod.Select);
        }

        private void ImageDraw_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            EnterMode(SLGameEngine.Enums.PaintMethod.Draw);
        }

        private void ImageDropper_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            EnterMode(SLGameEngine.Enums.PaintMethod.Dropper);
        }

        private void ImageFill_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            EnterMode(SLGameEngine.Enums.PaintMethod.Fill);
        }

        private void ImageErase_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            EnterMode(SLGameEngine.Enums.PaintMethod.Erase);
        }

        private void ImageGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _terrainMgr.ToggleGrid();
            SelectionBox.SetValue(Canvas.LeftProperty, (double)123);
        }

        private void ImageStopAllAnim_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _editor.StopAllAnimation();
        }

        private void ImageGameMode_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            // Toggle Game mode
            _gameMode = !_gameMode;
            _terrainMgr.ShowGrid(false);

            // Animate idle, looping
            _engine.LocalPlayer.AnimateInEditor = true;
            _engine.LocalPlayer.Animate(_engine.LocalPlayer.CurrentDirection, SLGameEngine.Enums.AnimationType.Idle, true);
        }

        private void Button_Click_Save(object sender, RoutedEventArgs e)
        {
            SaveMap();
        }
        private void Button_Click_Load(object sender, RoutedEventArgs e)
        {
            OpenMap();
        }

        private void ScalePickerSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (null != ScalePickerSlider)
            {
                _currentScaleValue = (int)ScalePickerSlider.Value;
                //Used to make slider snap to only int values 
                ScalePickerSlider.Value = _currentScaleValue;
                if (_currentScaleValue <= 0)
                    return;

                MacScale.ScaleX = MacScale.ScaleY = (double)_currentScaleValue / 100;
                MapArea.Width = _normalWidth * ((double)_currentScaleValue / 100);
                MapArea.Height = _normalHeight * ((double)_currentScaleValue / 100);
            }
        }

        private void mnuNewMap_Click(object sender, RoutedEventArgs e)
        {
            CreateNewMap();
        }

        private void mnuOpenMap_Click(object sender, RoutedEventArgs e)
        {
            OpenMap();

        }

        private void mnuSaveMap_Click(object sender, RoutedEventArgs e)
        {
            SaveMap();

        }
    }
}
